﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
using System.IO;
namespace ShipinJiami
{
    public delegate void SetValueHander(int value);
    public delegate void AddValueHander();
    public partial class MainFrm : Form
    {
        public List<FileInfo> Files = null;
        public SetValueHander SetValue;
        public AddValueHander AddValue;
        public string OpenPath = "";
        public string SavePath = "";
        public MainFrm()
        {
            InitializeComponent();
            SetValue = new SetValueHander(SetValueMethod);
            AddValue = new AddValueHander(AddValueMethod);
        }

        private void AddValueMethod()
        {
            if (MyBar.Value < MyBar.Maximum)
            {
                MyBar.Value += 1;
            }
            else
            {

            }
        }

        private void SetValueMethod(int value)
        {
            MyBar.Minimum = 0;
            MyBar.Maximum = value;
        }

        private void BtnJM_Click(object sender, EventArgs e)
        {

            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = "请选择要要加密的文件";
            if (fbd.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            OpenPath = fbd.SelectedPath;
            fbd.Description = "请选择要保存的地址";
            if (fbd.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            if (SavePath == OpenPath)
            {
                MessageBox.Show("加密和保存目录不能一样");
                return;
            }
            SavePath = fbd.SelectedPath + "\\";
            Files=new List<FileInfo>();
            Thread td = new Thread(BtnJMStart);
            td.IsBackground = true;
            td.Start();
            BtnJM.Enabled = false;
            BtnUnJM.Enabled = false;
        }

        private void BtnJMStart()
        {
            DirectoryInfo di = new DirectoryInfo(OpenPath);
            FileInfo[] files = di.GetFiles("*.*", SearchOption.AllDirectories);
            this.Invoke(SetValue, files.Length);
            foreach (var item in files)
            {
                if (item.FullName.IndexOf('.') == 0) {
                    continue;
                }
                Files.Add(item);
            }
            Thread td = new Thread(JM);
            td.IsBackground = true;
            td.Start();
        }
        private void JM()
        {
            while (true)
            {
                string path = "";
                lock (Files)
                {
                    if (Files.Count == 0)
                    {
                        Thread.Sleep(3000);
                        Application.Exit();
                        break;
                    }
                    path = Files[0].FullName;
                    Files.RemoveAt(0);
                }
                if (!string.IsNullOrEmpty(path))
                {
                    Common.EncryptFile(path, SavePath, "lainiao");
                }
                Thread.Sleep(500);
                this.Invoke(AddValue, null);
            }
        }

        private void BtnUnJM_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = "请选择要要解密的文件";
            if (fbd.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            OpenPath = fbd.SelectedPath;
            fbd.Description = "请选择要保存的地址";
            if (fbd.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            if (SavePath == OpenPath)
            {
                MessageBox.Show("解密和保存目录不能一样");
                return;
            }
            SavePath = fbd.SelectedPath + "\\";
            Files = new List<FileInfo>();
            Thread td = new Thread(BtnUnJMStart);
            td.IsBackground = true;
            td.Start();
            BtnJM.Enabled = false;
            BtnUnJM.Enabled = false;
        }


        private void BtnUnJMStart()
        {
            DirectoryInfo di = new DirectoryInfo(OpenPath);
            FileInfo[] files = di.GetFiles("*.LDATA", SearchOption.AllDirectories);
            this.Invoke(SetValue, files.Length);
            foreach (var item in files)
            {
                Files.Add(item);
            }
            Thread td = new Thread(UnJM);
            td.IsBackground = true;
            td.Start();
        }
        private void UnJM()
        {
            while (true)
            {
                string path = "";
                lock (Files)
                {
                    if (Files.Count == 0)
                    {
                        Thread.Sleep(3000);
                        Application.Exit();
                        break;
                    }
                    path = Files[0].FullName;
                    Files.RemoveAt(0);
                }
                if (!string.IsNullOrEmpty(path))
                {
                    Common.DecryptFile(path, SavePath, "lainiao");
                }
                Thread.Sleep(500);
                this.Invoke(AddValue, null);
            }
        }
    }
}
